//
//  ModifyProfileTableViewController.m
//  MingShi
//
//  Created by MJiao on 15/7/15.
//  Copyright (c) 2015年 MJiao. All rights reserved.
//

#import "ModifyProfileTableViewController.h"
#import "UserManager.h"
#import "CommonUtils.h"
#import "UIImageView+AFNetworking.h"
#import "AFHTTPRequestOperationManager.h"
#import "KVNProgress.h"
#import "VPImageCropperViewController.h"

#define ORIGINAL_MAX_WIDTH 640.0f


@interface ModifyProfileTableViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate, VPImageCropperDelegate>

@end

@implementation ModifyProfileTableViewController

- (IBAction)btnSaveAction:(id)sender {

    [KVNProgress showWithStatus:@"请求中..."];

    if (!self.hasSelectedNewAvatar) {
        [self processProfileSave:@""];
        return;
    }

    NSString *urlString = @"http://121.41.91.36:8888/api/users/upload";

    AFHTTPRequestOperationManager *httpManager = [[AFHTTPRequestOperationManager alloc] init];
    [httpManager.requestSerializer setTimeoutInterval:60];  //Time out after 25 seconds

    NSData *imageData = UIImageJPEGRepresentation(self.lbAvatar.image, 0.7);

    NSString *baseEncodedStr = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

    httpManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *userIdStr = [NSString stringWithFormat:@"%ld", [[[UserManager shareUserManager] curUser] userId]];
    NSDictionary *parameters = @{@"imageUrl" : baseEncodedStr, @"Id" : userIdStr};

    [httpManager POST:urlString
           parameters:parameters constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
            } success:^(AFHTTPRequestOperation *operation, id responseObject) {

                NSString *avatarImageUrl = [NSString stringWithCString:[responseObject bytes] encoding:NSUTF8StringEncoding];
                NSLog(@"avatarImageUrl:%@", avatarImageUrl);
                [[UserManager shareUserManager] curUser].imageUrl = avatarImageUrl;
                [self processProfileSave:avatarImageUrl];

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSLog(@"error:%@", error);
                // 结束登陆提示
                [KVNProgress dismiss];
                [KVNProgress showErrorWithStatus:@"请求失败,请重试"];
            }];


}

/**
* 处理个人信息报错
*/
- (void)processProfileSave:(NSString *)avatarImageUrl {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

    NSInteger userId = [[[UserManager shareUserManager] curUser] userId];
    NSString *userIdStr = [NSString stringWithFormat:@"%ld", userId];

    NSString *nickName = [CommonUtils serializableIdObject:[self.lbNickName text] defaultValue:@""];
    NSString *gradeName = [CommonUtils serializableIdObject:[self.lbGrade text] defaultValue:@""];
    NSString *brith = [CommonUtils serializableIdObject:[self.lbAge text] defaultValue:@""];
    NSString *slogins = [CommonUtils serializableIdObject:[self.lbSign text] defaultValue:@""];
    NSString *sex = [CommonUtils serializableIdObject:[self.lbSex text] defaultValue:@""];
    NSString *qq = [CommonUtils serializableIdObject:[self.lbQQ text] defaultValue:@""];
    NSString *address = [CommonUtils serializableIdObject:[self.lbAddress text] defaultValue:@""];
    NSString *schoolName = [CommonUtils serializableIdObject:[self.lbSchool text] defaultValue:@""];


    NSDictionary *parameters = @{
            @"userName" : nickName,
            @"Id" : userIdStr,
            @"gradeName" : gradeName,
            @"schoolName" : schoolName,
            @"brith" : brith,
            @"slogins" : slogins,
            @"sex" : sex,
            @"address" : address,
            @"qq" : qq
    };


    NSString *urlString = @"http://121.41.91.36:8888/api/users/register";
    [manager GET:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"responseObject:%@", responseObject);

        // 结束登陆提示
        [KVNProgress dismiss];

        NSString *code = [responseObject objectForKey:@"code"];
        //登录失败
        if (code == nil || ![code isEqualToString:@"00"]) {
            [KVNProgress showErrorWithStatus:[responseObject objectForKey:@"msg"]];
            return;
        }
        [[UserManager shareUserManager] curUser].userName = nickName;
        [[UserManager shareUserManager] curUser].gradeName = gradeName;
        @try {
            [[UserManager shareUserManager] curUser].age = [brith intValue];
        }
        @catch (NSException *exception) {
            NSLog(@"Exception occurred: %@, %@", exception, [exception userInfo]);
        }
        [[UserManager shareUserManager] curUser].sex = sex;
        [[UserManager shareUserManager] curUser].address = address;
        [[UserManager shareUserManager] curUser].qq = qq;

        [KVNProgress showSuccessWithStatus:[responseObject objectForKey:@"msg"] completion:^{
            [[UserManager shareUserManager] curUser].userName = nickName;
            [[UserManager shareUserManager] curUser].gradeName = gradeName;
            [[UserManager shareUserManager] curUser].schoolName = schoolName;

            @try {
                NSInteger age = [brith integerValue];
                [[UserManager shareUserManager] curUser].age = age;
            }
            @catch (NSException *exception1) {
                NSLog(@"Exception occurred: %@, %@", exception1, [exception1 userInfo]);
            }


            [[UserManager shareUserManager] curUser].slogins = slogins;
            [[UserManager shareUserManager] curUser].sex = sex;
            [[UserManager shareUserManager] curUser].address = address;
            [[UserManager shareUserManager] curUser].qq = qq;
        }];
    }    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        // 结束登陆提示
        [KVNProgress dismiss];
        [KVNProgress showErrorWithStatus:@"网络异常,请稍后再试"];
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    User *user = [[UserManager shareUserManager] curUser];
    [self.lbAccount setText:user.account];
    [self.lbAddress setText:user.address];
    [self.lbAcceptCount setText:[NSString stringWithFormat:@"%ld", user.cnnum]];
    [self.lbAge setText:[NSString stringWithFormat:@"%ld", user.age]];
    NSString *userImage = [CommonUtils serializableIdObject:user.imageUrl defaultValue:@""];
    [self.lbAvatar setImageWithURL:[NSURL URLWithString:userImage] placeholderImage:[UIImage imageNamed:@"defaultAvatar"]];
    [self.lbGrade setText:user.gradeName];
    [self.lbNickName setText:user.userName];
    [self.lbQQ setText:user.qq];
    [self.lbPostAount setText:[NSString stringWithFormat:@"%ld", user.twnum]];
    [self.lbSchool setText:user.schoolName];
    [self.lbSex setText:user.sex];
    [self.lbSign setText:[CommonUtils serializableIdObject:user.slogins defaultValue:@""]];
    [self.lbWenWen setText:[CommonUtils serializableIdObject:user.wenwen defaultValue:@""]];
    [self.lbGoldCount setText:[CommonUtils serializableIdObject:user.moneys defaultValue:@"0"]];
    //圆角
    self.lbAvatar.layer.masksToBounds = YES;
    self.lbAvatar.layer.cornerRadius = 25;
}


#pragma mark VPImageCropperDelegate

- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {

    self.lbAvatar.image = editedImage;
    //是否选择了新头像
    self.hasSelectedNewAvatar = YES;
//    self.portraitImageView.image = editedImage;
    [cropperViewController dismissViewControllerAnimated:YES completion:^{
        // TO DO
    }];
}

- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController {
    [cropperViewController dismissViewControllerAnimated:YES completion:^{
    }];
}

#pragma mark UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        // 拍照
        if ([self isCameraAvailable] && [self doesCameraSupportTakingPhotos]) {
            UIImagePickerController *controller = [[UIImagePickerController alloc] init];
            controller.sourceType = UIImagePickerControllerSourceTypeCamera;
            if ([self isFrontCameraAvailable]) {
                controller.cameraDevice = UIImagePickerControllerCameraDeviceFront;
            }
            NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
            [mediaTypes addObject:(__bridge NSString *) kUTTypeImage];
            controller.mediaTypes = mediaTypes;
            controller.delegate = self;
            [self presentViewController:controller
                               animated:YES
                             completion:^(void) {
                                 NSLog(@"Picker View Controller is presented");
                             }];
        }

    } else if (buttonIndex == 1) {
        // 从相册中选取
        if ([self isPhotoLibraryAvailable]) {
            UIImagePickerController *controller = [[UIImagePickerController alloc] init];
            controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
            [mediaTypes addObject:(__bridge NSString *) kUTTypeImage];
            controller.mediaTypes = mediaTypes;
            controller.delegate = self;
            [self presentViewController:controller
                               animated:YES
                             completion:^(void) {
                                 NSLog(@"Picker View Controller is presented");
                             }];
        }
    }
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:^() {
        UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        portraitImg = [self imageByScalingToMaxSize:portraitImg];
        // 裁剪
        VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:portraitImg cropFrame:CGRectMake(0, 100.0f, self.view.frame.size.width, self.view.frame.size.width) limitScaleRatio:3.0];
        imgEditorVC.delegate = self;
        [self presentViewController:imgEditorVC animated:YES completion:^{
            // TO DO
        }];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^() {
    }];
}

#pragma mark - UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

}

#pragma mark camera utility

- (BOOL)isCameraAvailable {
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}

- (BOOL)isRearCameraAvailable {
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}

- (BOOL)isFrontCameraAvailable {
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}

- (BOOL)doesCameraSupportTakingPhotos {
    return [self cameraSupportsMedia:(__bridge NSString *) kUTTypeImage sourceType:UIImagePickerControllerSourceTypeCamera];
}

- (BOOL)isPhotoLibraryAvailable {
    return [UIImagePickerController isSourceTypeAvailable:
            UIImagePickerControllerSourceTypePhotoLibrary];
}

- (BOOL)canUserPickVideosFromPhotoLibrary {
    return [self
            cameraSupportsMedia:(__bridge NSString *) kUTTypeMovie sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

- (BOOL)canUserPickPhotosFromPhotoLibrary {
    return [self
            cameraSupportsMedia:(__bridge NSString *) kUTTypeImage sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

- (BOOL)cameraSupportsMedia:(NSString *)paramMediaType sourceType:(UIImagePickerControllerSourceType)paramSourceType {
    __block BOOL result = NO;
    if ([paramMediaType length] == 0) {
        return NO;
    }
    NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:paramSourceType];
    [availableMediaTypes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSString *mediaType = (NSString *) obj;
        if ([mediaType isEqualToString:paramMediaType]) {
            result = YES;
            *stop = YES;
        }
    }];
    return result;
}

#pragma mark image scale utility

- (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage {
    if (sourceImage.size.width < ORIGINAL_MAX_WIDTH) return sourceImage;
    CGFloat btWidth = 0.0f;
    CGFloat btHeight = 0.0f;
    if (sourceImage.size.width > sourceImage.size.height) {
        btHeight = ORIGINAL_MAX_WIDTH;
        btWidth = sourceImage.size.width * (ORIGINAL_MAX_WIDTH / sourceImage.size.height);
    } else {
        btWidth = ORIGINAL_MAX_WIDTH;
        btHeight = sourceImage.size.height * (ORIGINAL_MAX_WIDTH / sourceImage.size.width);
    }
    CGSize targetSize = CGSizeMake(btWidth, btHeight);
    return [self imageByScalingAndCroppingForSourceImage:sourceImage targetSize:targetSize];
}

- (UIImage *)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize {
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        // center the image
        if (widthFactor > heightFactor) {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else if (widthFactor < heightFactor) {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    UIGraphicsBeginImageContext(targetSize); // this will crop
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if (newImage == nil) NSLog(@"could not scale image");

    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;
}

#pragma mark portraitImageView getter

- (UIImageView *)portraitImageView {
//    if (!_portraitImageView) {
////        CGFloat w = 100.0f; CGFloat h = w;
////        CGFloat x = (self.view.frame.size.width - w) / 2;
////        CGFloat y = (self.view.frame.size.height - h) / 2;
////        _portraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
////        [_portraitImageView.layer setCornerRadius:(_portraitImageView.frame.size.height/2)];
////        [_portraitImageView.layer setMasksToBounds:YES];
////        [_portraitImageView setContentMode:UIViewContentModeScaleAspectFill];
////        [_portraitImageView setClipsToBounds:YES];
////        _portraitImageView.layer.shadowColor = [UIColor blackColor].CGColor;
////        _portraitImageView.layer.shadowOffset = CGSizeMake(4, 4);
////        _portraitImageView.layer.shadowOpacity = 0.5;
////        _portraitImageView.layer.shadowRadius = 2.0;
////        _portraitImageView.layer.borderColor = [[UIColor blackColor] CGColor];
////        _portraitImageView.layer.borderWidth = 2.0f;
////        _portraitImageView.userInteractionEnabled = YES;
////        _portraitImageView.backgroundColor = [UIColor blackColor];
////        UITapGestureRecognizer *portraitTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(editPortrait)];
////        [_portraitImageView addGestureRecognizer:portraitTap];
//    }
//    return _portraitImageView;
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 1) {
        [self.lbSign sizeToFit];
        if (CGRectGetHeight(self.lbSign.frame) > 56 - 18 - 16) {
            return CGRectGetHeight(self.lbSign.frame) + 18 + 16;
        }
    }
    return 56;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //取消上次选择中的被选择效果
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:NO];

    long section = [indexPath section];
    long row = [indexPath row];

    if (section == 0) {
        switch (row) {
            case 0: {
                UIActionSheet *choiceSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                                         delegate:self
                                                                cancelButtonTitle:@"取消"
                                                           destructiveButtonTitle:nil
                                                                otherButtonTitles:@"拍照", @"从相册中选取", nil];
                [choiceSheet showInView:self.view];
            }
                break;
            case 1: {
                UIAlertController *alert = [UIAlertController
                        alertControllerWithTitle:@""
                                         message:@"请输入您的签名"
                                  preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction *action) {
                                                               //Do Some action here
                                                               UITextField *textField = alert.textFields[0];
                                                               NSLog(@"text was %@", textField.text);
                                                               [self.lbSign setText:textField.text];
                                                               [self.tableView reloadData];

                                                           }];
                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction *action) {

                                                                   NSLog(@"cancel btn");

                                                                   [alert dismissViewControllerAnimated:YES completion:nil];

                                                               }];

                [alert addAction:ok];
                [alert addAction:cancel];

                [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                    textField.placeholder = @"请输入您的签名";
                }];

                [self presentViewController:alert animated:YES completion:nil];
            }
                break;
            case 2: {
                UIAlertController *alert = [UIAlertController
                        alertControllerWithTitle:@""
                                         message:@"请输入您的昵称"
                                  preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction *action) {
                                                               //Do Some action here
                                                               UITextField *textField = alert.textFields[0];
                                                               NSLog(@"text was %@", textField.text);
                                                               [self.lbNickName setText:textField.text];

                                                           }];
                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction *action) {

                                                                   NSLog(@"cancel btn");

                                                                   [alert dismissViewControllerAnimated:YES completion:nil];

                                                               }];

                [alert addAction:ok];
                [alert addAction:cancel];

                [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                    textField.placeholder = @"请输入您的昵称";
                }];

                [self presentViewController:alert animated:YES completion:nil];
            }
                break;
        }
    } else if (section == 1) {
        switch (row) {
            case 0: {
                UIAlertController *alert = [UIAlertController
                        alertControllerWithTitle:@""
                                         message:@"请选择性别"
                                  preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction *ok = [UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction *action) {
                                                               [self.lbSex setText:@"男"];
                                                               [alert dismissViewControllerAnimated:YES completion:nil];

                                                           }];
                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction *action) {

                                                                   [self.lbSex setText:@"女"];

                                                                   [alert dismissViewControllerAnimated:YES completion:nil];

                                                               }];

                [alert addAction:ok];
                [alert addAction:cancel];


                [self presentViewController:alert animated:YES completion:nil];
            }
                break;
            case 1: {
                //自己定义一个UITextField添加上去，后来发现ios5自带了此功能
//                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"类别修改" message:@" " delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"修改",nil];
//                UITextField * txt = [[UITextField alloc] init];
//                txt.backgroundColor = [UIColor whiteColor];
//                txt.frame = CGRectMake(alert.center.x+65,alert.center.y+48, 150,23);
//                [alert addSubview:txt];
//                [alert show];
                // use UIAlertController
                UIAlertController *alert = [UIAlertController
                        alertControllerWithTitle:@""
                                         message:@"请输入年龄"
                                  preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction *action) {
                                                               //Do Some action here
                                                               UITextField *textField = alert.textFields[0];
                                                               NSLog(@"text was %@", textField.text);
                                                               [self.lbAge setText:textField.text];

                                                           }];
                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction *action) {

                                                                   NSLog(@"cancel btn");

                                                                   [alert dismissViewControllerAnimated:YES completion:nil];

                                                               }];

                [alert addAction:ok];
                [alert addAction:cancel];

                [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                    textField.placeholder = @"请输入年龄";
                    textField.keyboardType = UIKeyboardTypeNumberPad;
                }];

                [self presentViewController:alert animated:YES completion:nil];
            }
                break;
            case 2: {
                UIAlertController *alert = [UIAlertController
                        alertControllerWithTitle:@""
                                         message:@"请输入您的QQ号码"
                                  preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction *action) {
                                                               //Do Some action here
                                                               UITextField *textField = alert.textFields[0];
                                                               NSLog(@"text was %@", textField.text);
                                                               [self.lbQQ setText:textField.text];

                                                           }];
                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction *action) {

                                                                   NSLog(@"cancel btn");

                                                                   [alert dismissViewControllerAnimated:YES completion:nil];

                                                               }];

                [alert addAction:ok];
                [alert addAction:cancel];

                [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                    textField.placeholder = @"请输入您的QQ号码";
                    textField.keyboardType = UIKeyboardTypeNumberPad;
                }];

                [self presentViewController:alert animated:YES completion:nil];
            }
                break;
            case 3: {

                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择您的年级" message:@"" preferredStyle:UIAlertControllerStyleAlert];


                UIAlertAction *xiaoXue = [UIAlertAction actionWithTitle:@"小学" style:UIAlertActionStyleDefault
                                                                handler:^(UIAlertAction *action) {
                                                                    [self.lbGrade setText:@"小学"];

                                                                }];
                UIAlertAction *chuYi = [UIAlertAction actionWithTitle:@"初一" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction *action) {

                                                                  [self.lbGrade setText:@"初一"];
                                                              }];
                UIAlertAction *chuEr = [UIAlertAction actionWithTitle:@"初二" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction *action) {

                                                                  [self.lbGrade setText:@"初二"];
                                                              }];
                UIAlertAction *chuSan = [UIAlertAction actionWithTitle:@"初三" style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction *action) {
                                                                   [self.lbGrade setText:@"初三"];

                                                               }];
                UIAlertAction *gaoYi = [UIAlertAction actionWithTitle:@"高一" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction *action) {

                                                                  [self.lbGrade setText:@"高一"];
                                                              }];
                UIAlertAction *gaoEr = [UIAlertAction actionWithTitle:@"高二" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction *action) {

                                                                  [self.lbGrade setText:@"高二"];
                                                              }];
                UIAlertAction *gaoSan = [UIAlertAction actionWithTitle:@"高三" style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction *action) {
                                                                   [self.lbGrade setText:@"高三"];

                                                               }];
                [alert addAction:xiaoXue];
                [alert addAction:chuYi];
                [alert addAction:chuEr];
                [alert addAction:chuSan];
                [alert addAction:gaoYi];
                [alert addAction:gaoEr];
                [alert addAction:gaoSan];
                [self presentViewController:alert animated:YES completion:nil];
            }
                break;
            case 4: {
                UIAlertController *alert = [UIAlertController
                        alertControllerWithTitle:@""
                                         message:@"请输入您学校名称"
                                  preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction *action) {
                                                               //Do Some action here
                                                               UITextField *textField = alert.textFields[0];
                                                               NSLog(@"text was %@", textField.text);
                                                               [self.lbSchool setText:textField.text];

                                                           }];
                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction *action) {

                                                                   NSLog(@"cancel btn");

                                                                   [alert dismissViewControllerAnimated:YES completion:nil];

                                                               }];

                [alert addAction:ok];
                [alert addAction:cancel];

                [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                    textField.placeholder = @"请输入您学校名称";
                }];
                            [self presentViewController:alert animated:YES completion:nil];

            }
                break;
            case 5: {
                UIAlertController *alert = [UIAlertController
                        alertControllerWithTitle:@""
                                         message:@"请输入您学校地址"
                                  preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction *action) {
                                                               //Do Some action here
                                                               UITextField *textField = alert.textFields[0];
                                                               NSLog(@"text was %@", textField.text);
                                                               [self.lbSchool setText:textField.text];

                                                           }];
                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                               handler:^(UIAlertAction *action) {

                                                                   NSLog(@"cancel btn");

                                                                   [alert dismissViewControllerAnimated:YES completion:nil];

                                                               }];

                [alert addAction:ok];
                [alert addAction:cancel];

                [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                    textField.placeholder = @"请输入您学校地址";
                }];

                [self presentViewController:alert animated:YES completion:nil];
            }
                break;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
